package emulator.assembler;

import emulator.hardware.HwByte;
import emulator.hardware.nmos6502.Command;
import emulator.hardware.nmos6502.Operand;
import emulator.util.MemoryPtr;

public class Instruction {
	private byte[] raw_bytes;
	private String opcode;
	private String operand;
	
	public Instruction(MemoryPtr mem_ref, Command command, Operand operand, SymbolTable symbols)
	{
		opcode = command.getName();
		raw_bytes = new byte[1+operand.getByteCount()];
		mem_ref.get(raw_bytes);
		this.operand = operand.getString(mem_ref, symbols);
	}
	
	public Instruction(MemoryPtr mem_ref, int data_length)
	{
		raw_bytes = new byte[data_length];
		mem_ref.get(raw_bytes);
		opcode = ".by";
		operand = getBytes();
	}
	
	/**
	 * @return the raw_bytes
	 */
	public String getBytes() {
		String result = new HwByte(raw_bytes[0]).toString();
		for (int i = 1; i < raw_bytes.length; i++)
			result += " " + new HwByte(raw_bytes[i]).toString();
		return result;
	}

	/**
	 * @return the raw_bytes
	 */
	public String getBytesShort() {
		String result = new HwByte(raw_bytes[0]).toString();
		int max_i = (raw_bytes.length > 3 ? 2 : raw_bytes.length); 
		for (int i = 1; i < max_i; i++)
			result += " " + new HwByte(raw_bytes[i]).toString();
		if (max_i < raw_bytes.length)
			result += "...";
		return result;
	}

	/**
	 * @return the opcode
	 */
	public String getOpcode() {
		return opcode;
	}

	/**
	 * @return the operand
	 */
	public String getOperand() {
		return operand;
	}

	public int getByteCount() {
		return raw_bytes.length;
	}

	public boolean isEnd() {
		return	opcode.equals("RTS") ||
				opcode.equals("JMP") ||
				opcode.equals("RTI") ||
				opcode.equals("BRK") ||
				opcode.equals("ILL");
	}
}
